// pages/my/shopping_cart/shopping_cart.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    carArray: [
      {
        carImage: '../../../images/imgMall/j1.jpg',
        carTitle: '数据结构（C语言版）',
        carPrice: '35.00',
        carNum: 1,
        carShow: true
      },
      {
        carImage: '../../../images/imgMall/c3.jpg',
        carTitle: 'C语言程序设计（第五版）',
        carPrice: '30.00',
        carNum: 1,
        carShow: true
      },
      {
        carImage: '../../../images/imgMall/c2.jpg',
        carTitle: 'C语言从入门到精通',
        carPrice: '50.00',
        carNum: 1,
        carShow: true
      }
    ]
  },

 
  onLoad: function (options) {
    this.getCartBook();
  },

  getCartBook:function(){
    var userid =  getApp().globalData.userid;
    //console.log("userid:"+userid);
    var mark = 'getCartBook';
    wx.request({
      url: 'http://localhost:8080/youYanZaiXian/MallServlet',
      method: 'POST',
      data:{
        mark:mark,
        userid:userid,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Accept': 'application/json'
      },
      success: (res)=>{
        var cartList = res.data.data.cartList;
        this.setData({
          cartList:cartList,
        })
        //console.log(this.data.cartList)
        this.processCartDate();  // 给cartList添加carshow属性
        this.calSumPrice();
        var cartAmount = cartList.length;
        //console.log("count:"+count);
        this.setData({
          cartAmount:cartAmount,
        });
      }
    });
  },

  // 计算购物车中的总价格
  calSumPrice:function(){
    var cartList = this.data.cartList;
    var sumPrice = 0;
    for(var i=0;i<cartList.length;i++){
      sumPrice = sumPrice+cartList[i].bookPrice;
    }
    //console.log("sumPrice:"+sumPrice);
    this.setData({
      sumPrice:sumPrice
    })
  },

  // 给cartList添加carShow属性
  processCartDate:function(){
    var cartList = this.data.cartList;
    for(var i=0;i<cartList.length;i++){
      var key = "carShow";
      var value = true;
      cartList[i][key] = value;
    }
    this.setData({
      cartList:cartList,
    })
    //console.log(cartList);
  },

  carRemove: function(event){
    var that = this;
    var index = event.target.dataset.alphaBeta;
    var key = 'cartList['+index+'].carShow';
    var obj = {};
    obj[key] = false;
    this.setData(obj);
  
    var scid = event.currentTarget.dataset.scid;
    //console.log("scid:"+scid);
    var mark = 'carRemove';
      wx.request({
        url: 'http://localhost:8080/youYanZaiXian/MallServlet',
        method: 'POST',
        data:{
          mark:mark,
          scid:scid,
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Accept': 'application/json'
        },
        success: (res)=>{
          var suc = res.data.data.suc;
          if(suc==1){
            wx.showToast({
              title: '删除成功',
              icon: 'success',
              duration: 2000
            })
          }else{
            wx.showToast({
              title: '删除失败',
              icon: 'error',
              duration: 2000
            })
          }
          this.getCartBook();  // 删除后重新调用获取购物车数据
        }
      });
  },

  // --------------------页面跳转、切换模块 start------------------------
goBookDetail:function(e){
  var book = e.currentTarget.dataset.book;
  var cartAmount = this.data.cartAmount;
  //console.log(book);
  var bookJson = JSON.stringify(book);
  wx.navigateTo({
    url: '../../spdetail/spdetail?book='+bookJson+'&cartAmount='+cartAmount,
  })
},

toPay: function(){
  var cartList = this.data.cartList;
  var cartListJson = JSON.stringify(cartList);
  var sumPrice = this.data.sumPrice;
  console.log("sumPrice:"+sumPrice);
  console.log(cartList);
    wx.navigateTo({
      url: '../../pay/pay?sumPrice='+sumPrice+'&cartList='+cartListJson,
    })
}
// --------------------页面跳转、切换模块 end------------------------
})